/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package toesp.bean;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import toesp.entity.Kuliah;

/**
 *
 * @author satria.sp
 */
public class KuliahBean {
    private EntityManagerFactory emf;

    public KuliahBean() {
        emf = Persistence.createEntityManagerFactory("swing-researchPU");
    }

    private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    
    private void getCloseEntityManager() {
        emf.close();
    }
    
    public boolean add(Kuliah o) throws IllegalStateException, SystemException{
        boolean bolRet = false;
        Transaction tx = null ; 
        
        try { 
            EntityManager em = getEntityManager();
            if(this.getOnebyId(o.getKuliahId())==null){
                em.getTransaction().begin();
                em.persist(o);
                em.getTransaction().commit();
                bolRet = true;
            }
        } catch (Exception e) { 
            tx.rollback(); 
        }finally{
            //this.getCloseEntityManager();
        }
        
        return bolRet;
    }
    
    public boolean edit(Kuliah o) throws IllegalStateException, SystemException{
        Transaction tx = null;
        boolean bolRet = false; 
        
        try {
            EntityManager em = getEntityManager();
            if(this.getOnebyId(o.getKuliahId())!=null){
                em.getTransaction().begin();
                em.merge(o);
                em.getTransaction().commit();
                bolRet = true;
            }
        } catch (Exception e) {
            tx.rollback();
        }finally{
            //this.getCloseEntityManager();
        }
        return bolRet;
    }
    
    public boolean del(String p_strKulId) throws IllegalStateException, SystemException{
        boolean bolRet = false;
        Transaction tx = null; 
        
        try {
            EntityManager em = getEntityManager();
            Kuliah o = this.getOnebyId(p_strKulId);
            if(o!=null){
                em.getTransaction().begin();
                em.remove(o);
                em.getTransaction().commit();
                bolRet = true;
            }
        } catch (Exception e) {
            tx.rollback();
        }finally{
            //this.getCloseEntityManager();
        }
        return bolRet;
    }
             
    @SuppressWarnings("unchecked")
    public List<Kuliah> getAll() throws Exception {
        List<Kuliah> lstPg = null; 
        
        try { 
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Kuliah o ORDER BY o.kuliahNama ASC");   
            lstPg = q.getResultList();
        } catch (Exception e) {            
        }finally{
            //this.getCloseEntityManager();
        }  
        
        return lstPg;
    }
    
    public int count() {
        List lstCount = null; 
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT COUNT(o) FROM Kuliah o");
            lstCount = q.getResultList();
        } finally {
             //this.getCloseEntityManager();
        }
        return ((Long) lstCount.get(0)).intValue();
    }
    
    @SuppressWarnings("unchecked")
    public List<Kuliah> getPaging(int p_intStart, int p_intMax) throws Exception {
        List<Kuliah> lstPg = null;
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Kuliah o ORDER BY o.kuliahNama ASC");
            q.setFirstResult(p_intStart);
            q.setMaxResults(p_intMax);
            lstPg = q.getResultList();
        } finally {
            //this.getCloseEntityManager();
        }
        
        return lstPg;
    }
    
    public int countFind(String p_strFind) {
        List lstCount; 
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT count(o) FROM Kuliah o WHERE o.kuliahNama LIKE :p_strNama ORDER BY o.kuliahNama ASC");
            q.setParameter("p_strNama","%"+p_strFind+"%");
            lstCount = q.getResultList();
        } finally {
             //this.getCloseEntityManager();
        }
        return ((Long) lstCount.get(0)).intValue();
    }
    
    @SuppressWarnings("unchecked")
    public List<Kuliah> getFindByNamePaging(String p_strFind,int p_intStart, int p_intMax) throws Exception {
        List<Kuliah> lstPg = null;
        try {
            EntityManager em = getEntityManager();
            Query q =em.createQuery("SELECT o FROM Kuliah o WHERE o.kuliahNama LIKE :p_strNama ORDER BY o.kuliahNama ASC");
            q.setParameter("p_strNama","%"+p_strFind+"%");
            q.setFirstResult(p_intStart);
            q.setMaxResults(p_intMax);
            lstPg = q.getResultList();
        } finally {
            //this.getCloseEntityManager();
        }
        
        return lstPg;
    }
    
    public Kuliah getOnebyId(String p_strKulId) throws Exception {
        Kuliah j = null;
        try {          
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Kuliah o WHERE o.kuliahId=:p_strKulId");   
            q.setParameter("p_strKulId", p_strKulId);            
            @SuppressWarnings("unchecked")
            List<Kuliah> lstPg = q.getResultList();
            j = lstPg.get(0);
        } catch (Exception e) {            
        }finally{          
            //this.getCloseEntityManager();
        }  
        return j;
    }
    
    public Kuliah getOnebyName(String p_strKulNama) throws Exception {
        Kuliah j = null;
        try {          
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Kuliah o WHERE o.kuliahNama=:p_strKulNama");   
            q.setParameter("p_strKulNama", p_strKulNama);            
            @SuppressWarnings("unchecked")
            List<Kuliah> lstPg = q.getResultList();
            j = lstPg.get(0);
        } catch (Exception e) {            
        }finally{          
            //this.getCloseEntityManager();
        }  
        return j;
    }
    
    public void commitaj(){
        
    }
}
